package com.gdsdxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdsdxy.dto.ResultVo;
import com.gdsdxy.entity.User;
import com.gdsdxy.entity.UserFriends;
import com.gdsdxy.mapper.UserFriendsMapper;
import com.gdsdxy.service.IUserFriendsService;
import com.gdsdxy.service.IUserService;
import com.gdsdxy.utils.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserFriendsServiceImpl extends ServiceImpl<UserFriendsMapper, UserFriends> implements IUserFriendsService {
    @Resource
    private UserFriendsMapper userFriendsMapper;
    @Resource
    private IUserService userService;

    @Override
    public ResultVo findUserFriend(Long id) {
        UserFriends byId = getById(id);
        return byId !=null ? ResultVo.ok(byId):ResultVo.fail("用户好友不存在！");
    }

    @Override
    public ResultVo addUserFriend(UserFriends userFriends) {
        Long fid = userFriends.getFid();
        User byId = userService.getById(fid);
        //判断用户好友是否存在
        if(byId==null){
            return ResultVo.fail("用户好友不存在！");
        }
        boolean result = save(userFriends);
        return result ? ResultVo.ok("添加成功"):ResultVo.fail("添加失败");
    }

    @Override
    public ResultVo updateUserFriend(UserFriends userFriends) {
        //先获取用户id
        Long userId = UserHolder.getUser().getId();
        if (userFriends.getUid() != userId) {
            return ResultVo.fail("用户好友不存在！");
        }
        //获取好友id
        Long userFriendsFid = userFriends.getFid();
        UserFriends friends = query().eq("uid", userId).eq("fid", userFriendsFid).one();
        if (friends==null) {
            return ResultVo.fail("用户好友不存在！");
        }
        boolean result = updateById(userFriends);
        return result ? ResultVo.ok("修改成功"):ResultVo.fail("修改失败");
    }

    @Override
    public ResultVo deleteUserFriend(Long id) {
        boolean result = removeById(id);
        return result ? ResultVo.ok("删除成功"):ResultVo.fail("删除失败");
    }
}
